
/**
 * https://leetcode.cn/problems/minimum-size-subarray-sum/submissions/559939867/
 * 209. 长度最小的子数组
 * medium, 陈贝纯 2024.08.30
 * 滑动窗口
 */


class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int sum=0;
        int l=0;
        int leng=0;
        for(int i=0;i<nums.size();++i){
            sum+=nums[i];
            if(sum>=target){
                while(l<=i&&sum>=target){
                sum-=nums[l];
                l++;
                }
               if(leng==0||leng>i-l+2)
               leng=i-l+2;
            }
        }
        return leng;
    }
};